/* eslint-disable max-lines */
import React from 'react';
import {Spin, Card, Button, Row, Col, Table, Form, Message as msg, Collapse} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {FORM_OPTIONS, TABLE, FIXED_COLUMN_WIDTH, FORM_ROW_OPTIONS} from '../constants';
import {getDetail} from './api';
import SaleShipmentDetailSelectPanel from './SaleShipmentDetailSelectPanel';
import PartSelectPanel from './PartSelectPanel';
import ProductSelectPanel from './ProductSelectPanel';
import PartOneSelectPanel from './PartOneSelectPanel';
import {kDClaimType, claimCompensationWay, urgencyLevel, claimPartType, claimTransportType, isYesNo} from '../Enum';
import TextInput from 'Shared/components/TextInput';
import SearchInput from 'Shared/components/SearchInput';
import SaleShipmentSelectPanel from './SaleShipmentSelectPanel';
import {injectIntl, language, formatMessage} from './intl';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import FileUpload from './FileUpload';
import WrappedDatePicker from '../common/WrappedDatePicker';
import WrappedInputNumber from '../common/WrappedInputNumber';
import WrappedSelect from '../common/WrappedSelect';
import styles from './style.css';
import Return from '../common/Return';
import {newOrUpdateOrderValidator} from './validator';
const {Panel} = Collapse;
const FormItem = Form.Item;

const kDClaimTypeToList = kDClaimType.toList(language);
const claimCompensationWayToList = claimCompensationWay.toList(language);
const urgencyLevelToList = urgencyLevel.toList(language);
const claimPartTypeToList = claimPartType.toList(language);
const claimTransportTypeToList = claimTransportType.toList(language);
const isYesNoToList = isYesNo.toList(language);
class AddPanel extends React.PureComponent {
    state = {
        loading: false,
        hasOperat: false,
        isValidate: false,
        showSaleShipmentDetailSelectPanel: false,
        showPartSelectPanel: false,
        showSaleShipmentSelectPanel: false,
        showProductSelectPanel: false,
        show2ProductSelectPanel: false,
        showPartOneSelectPanel: false,
        data: {
            urgencyLevel: 1,
            claimType: 1,
            claimPartType: 1,
            requireCompensationWay: 2,
            requireTransportType: 1,
        },
        details: [],
        detailId: '',
        partDetailId: '',
    }
    componentDidMount() {
        if(this.props.id) {
            this.setState({loading: true});
            getDetail(this.props.id)
                .then(res => {
                    if(res.ok)
                        this.setState({
                            loading: false,
                            data: res.data,
                            details: res.data.details,
                        });
                    else
                        this.setState({loading: false});
                });
        }
    }
    componentWillUnmount() {
        this.isunmount = true;
    }
    // eslint-disable-next-line react/sort-comp
    onChange = (value, name) => {
        const data = Object.assign({}, this.state.data, {
            [name]: value
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    onClickOkOnPopconfirm = () => {
        this.props.onCancel();
    }
    onSubmit = () => {
        const {data, details} = this.state;
        const {brands, enterprise} = this.props.initEnumData;
        const brandIdId = brands && brands.length === 1 ? brands[0].value : data.brandId;
        const isValid = newOrUpdateOrderValidator(data, details, brandIdId, enterprise);
        if(!isValid) {
            this.setState({
                isValidate: true
            });
            return;
        }
        this.setState({loading: true});
        this.props.onSubmit(data, details, brandIdId, enterprise).then(() => {
            if(this.success)
                this.props.successReturn();
            if(!this.isunmount)
                this.setState({loading: false});
        });
    }
    onClickSearchSaleShipmentBtn = () => {
        const {brands, enterprise} = this.props.initEnumData;
        const brandIdId = brands && brands.length === 1 ? brands[0].value : this.state.data.brandId;
        if(!brandIdId || !enterprise.dealerId)
            return msg.warn(formatMessage({
                id: 'addPanel.message.brandIdandDealerId',
                defaultMessage: '请先选择品牌和总代'
            }));
        this.setState({
            showSaleShipmentSelectPanel: true
        });
    }
    onClickSearchProductBtn = id => {
        const {brands, enterprise} = this.props.initEnumData;
        const brandIdId = brands && brands.length === 1 ? brands[0].value : this.state.data.brandId;
        if(!brandIdId || !enterprise.dealerId)
            return msg.warn(formatMessage({
                id: 'addPanel.message.brandIdandDealerId',
                defaultMessage: '请先选择品牌和总代'
            }));
        this.setState({
            detailId: id,
            showProductSelectPanel: true
        });
    }
    onCloseSaleShipmentSelectPanel = () => {
        this.setState({showSaleShipmentSelectPanel: false});
    }
    onSelectSaleShipment = records => {
        this.setState({
            data: Object.assign({}, this.state.data, {
                saleShipmentId: records.id,
                saleShipmentCode: records.code,
                batchNumber: records.batchNumber,
            }),
            hasOperat: true,
            showSaleShipmentSelectPanel: false
        });
    }
    onClickAddDetailBtn = () => {
        this.setState({
            showSaleShipmentDetailSelectPanel: !this.state.showSaleShipmentDetailSelectPanel
        });
    }
    onClickAddPartDetailBtn = e => {
        e.stopPropagation();
        const detail = this.state.details.find(v => v.id === e.target.id);
        this.setState({
            currentSelectRowkeys: detail.partDetails,
            detailId: e.target.id,
            showPartSelectPanel: true
        });
    }
    onClickClosePartDetailBtn = () => {
        this.setState({
            showPartSelectPanel: false
        });
    }
    onClickAddProductDetailBtn = () => {
        this.setState({
            showProductSelectPanel: !this.state.showProductSelectPanel
        });
    }
    onSelectSaleShipmentDetail = value => {
        const details = this.state.details;
        value.map(p => {
            const detail = this.state.details.find(d => d.id === p.id);
            if(!detail) {
                const detail = {
                    id: p.id,
                    kdBomCode: p.kdBomCode,
                    quantity: p.quantity,
                    partDetails: []
                };
                details.push(detail);
            }
            return p;
        });
        this.setState({
            showSaleShipmentDetailSelectPanel: false,
            hasOperat: true,
            details
        });
    }
    onSelectProduct = records => {
        const details = this.state.details || [];
        const id = this.state.detailId;
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    productCategoryId: records.id,
                    productCategoryName: records.name,
                    engineModel: records.engineModelName,
                    transmissionModel: records.gearboxModel,
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true,
            showProductSelectPanel: false
        });
    }
    onSelectPart = records => {
        const details = this.state.details || [];
        const id = this.state.detailId;
        const detail = details.find(d => d.id === id);
        records.forEach(r => {
            const index = detail.partDetails.findIndex(v => v.shipPartId === r.id);
            if(index === -1)
                detail.partDetails.push({
                    detailId: id,
                    quantity: 1,
                    shipPartId: r.id,
                    shipPartCode: r.code,
                    shipPartName: r.name,
                });
        });
        this.setState({
            showPartSelectPanel: false,
            hasOperat: true,
            details
        });
    }
    onClickPartDetailBtn = r => {
        this.setState({
            detailId: r.detailId,
            partDetailId: r.shipPartId,
            showPartOneSelectPanel: true
        });
    }
    onClickOneDetailCloseBtn = () => {
        this.setState({
            showPartOneSelectPanel: false
        });
    }
    onSelectPartDetail = records => {
        const details = this.state.details || [];
        const detail = details.find(d => d.id === this.state.detailId);
        const tmp = Object.assign({}, detail, {
            partDetails: detail.partDetails.map(v => {
                if(v.shipPartId === this.state.partDetailId)
                    return Object.assign({}, v, {
                        partAfterId: records.id,
                        partCodeAfter: records.code,
                        partNameAfter: records.name
                    });
                return v;
            })
        });
        const olddetails = this.state.details.filter(p => p.id !== this.state.detailId);
        this.setState({
            details: [
                ...olddetails,
                tmp
            ],
            hasOperat: true,
            showPartOneSelectPanel: false
        });
    }
    onDeleteDetail = e => {
        const id = e.target.id;
        const details = this.state.details.filter(p => p.id !== id);
        this.setState({
            details,
            hasOperat: true
        });
    }
    onChangeQuantity = (quantity, id) => {
        const details = this.state.details || [];
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    quantity,
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    onChangeAll = (value, id, e) => {
        const details = this.state.details || [];
        const name = e.target.name;
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    [name]: value
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    onUpdateisFromKDBOM = (r, isFromKDBOM) => {
        const details = this.state.details || [];
        const detail = details.find(d => d.id === r.detailId);
        const tmp = Object.assign({}, detail, {
            partDetails: detail.partDetails.map(v => {
                if(v.shipPartId === r.shipPartId)
                    return Object.assign({}, v, {
                        isFromKDBOM
                    });
                return v;
            })
        });
        const olddetails = this.state.details.filter(p => p.id !== r.detailId);
        this.setState({
            details: [
                ...olddetails,
                tmp
            ],
            hasOperat: true
        });
    }
    onUpdateisSubPart = (r, isSubPart) => {
        const details = this.state.details || [];
        const detail = details.find(d => d.id === r.detailId);
        const tmp = Object.assign({}, detail, {
            partDetails: detail.partDetails.map(v => {
                if(v.shipPartId === r.shipPartId)
                    return Object.assign({}, v, {
                        isSubPart
                    });
                return v;
            })
        });
        const olddetails = this.state.details.filter(p => p.id !== r.detailId);
        this.setState({
            details: [
                ...olddetails,
                tmp
            ],
            hasOperat: true
        });
    }
    onChangeSupplierCode = (r, supplierCode) => {
        const details = this.state.details || [];
        const detail = details.find(d => d.id === r.detailId);
        const tmp = Object.assign({}, detail, {
            partDetails: detail.partDetails.map(v => {
                if(v.shipPartId === r.shipPartId)
                    return Object.assign({}, v, {
                        supplierCode
                    });
                return v;
            })
        });
        const olddetails = this.state.details.filter(p => p.id !== r.detailId);
        this.setState({
            details: [
                ...olddetails,
                tmp
            ],
            hasOperat: true
        });
    }
    onDeletePartDetail = r => {
        const details = this.state.details || [];
        const detail = details.find(d => d.id === r.detailId);
        const tmp = Object.assign({}, detail, {
            partDetails: detail.partDetails.filter(p => p.shipPartId !== r.shipPartId)
        });
        const olddetails = this.state.details.filter(p => p.id !== r.detailId);
        this.setState({
            details: [
                ...olddetails,
                tmp
            ],
            hasOperat: true
        });
    }
    removeFilePath = file => {
        const data = this.state.data;
        const index = data.claimDetails.findIndex(item => item.fileId === file.fileId);
        if(index === -1)
            return null;
        const claimDetails = data.claimDetails || [];
        claimDetails.splice(index, 1);
        this.setState({
            data: {
                ...data,
                claimDetails
            },
            hasOperat: true,
        });
    }
    onChangeUpload = value => {
        const data = this.state.data;
        const claimDetails = data.claimDetails || [];
        claimDetails.push(value[0]);
        this.setState({
            data: {
                ...data,
                claimDetails
            },
            hasOperat: true,
        });
    }
    labelMap = d => {
        if(d.id === undefined)
            return '';
        if(d)
            return `${d.code}`;
        return '';
    }
    onUpdateQuantity = (quantity, r) => {
        const details = this.state.details || [];
        const detail = details.find(d => d.id === r.detailId);
        const tmp = Object.assign({}, detail, {
            partDetails: detail.partDetails.map(v => {
                if(v.shipPartId === r.shipPartId)
                    return Object.assign({}, v, {
                        quantity
                    });
                return v;
            })
        });
        const olddetails = this.state.details.filter(p => p.id !== r.detailId);
        this.setState({
            details: [
                ...olddetails,
                tmp
            ],
            hasOperat: true
        });
    }
    render() {
        const {brands, enterprise} = this.props.initEnumData;
        const {data, details = {}, isValidate, loading, currentSelectRowkeys = []} = this.state;
        const brandIdId = brands && brands.length === 1 ? brands[0].value : data.brandId;
        if(brands.length === 1 && !this.props.id) {
            data.brandId = brands[0].value;
            data.brandName = brands[0].text;
        }
        const countryInfo = enterprise ? enterprise.country : '';
        const dealerInfo = enterprise ? `(${enterprise.dealerCode})${enterprise.dealerName}` : '';
        const partColumns = [{
            title: <span className="ant-form-item-required">
                {formatMessage({
                    id: 'detailPanel.column.shipPartCode',
                    defaultMessage: '发运单零件号'
                })}</span>,
            dataIndex: 'shipPartCode',
        }, {
            title: <span className="ant-form-item-required">
                {formatMessage({
                    id: 'detailPanel.column.shipPartName',
                    defaultMessage: '发运零件名称'
                })}</span>,
            dataIndex: 'shipPartName',
        }, {
            title: <span className="ant-form-item-required">
                {formatMessage({
                    id: 'detailPanel.colums.quantity',
                    defaultMessage: '索赔数量'
                })}
            </span>,
            dataIndex: 'quantity',
            render: (text, record) => {
                const validate = (!text || text === undefined) && text !== 0 ? {validateStatus: 'error'} : null;
                return (
                    <FormItem {...validate}>
                        <WrappedInputNumber min={0}
                            precision={0}
                            name="quantity"
                            value={text} onBlur={value => this.onUpdateQuantity(value, record)}/>
                    </FormItem>
                );
            }
        }, {
            title: formatMessage({
                id: 'detailPanel.column.partCodeAfter',
                defaultMessage: '更改后零件号'
            }),
            dataIndex: 'partCodeAfter',
            render: (text, record) => (
                <FormItem>
                    <SearchInput
                        id={record.shipPartId}
                        name="partCodeAfter"
                        value={text}
                        onClickSearchBtn={() => this.onClickPartDetailBtn(record)}/>
                </FormItem>
            )
        }, {
            title: formatMessage({
                id: 'detailPanel.column.partNameAfter',
                defaultMessage: '更改后零件名称'
            }),
            dataIndex: 'partNameAfter',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.isFromKDBOM',
                defaultMessage: '是否来自KDBOM'
            }),
            dataIndex: 'isFromKDBOM',
            render: (text, record) => (
                <FormItem>
                    <WrappedSelect
                        allowClear
                        id={record.id}
                        options={isYesNoToList}
                        value={text}
                        onChange={v => this.onUpdateisFromKDBOM(record, v)}/>
                </FormItem>
            )
        }, {
            title: formatMessage({
                id: 'detailPanel.column.isSubPart',
                defaultMessage: '是否SUB件'
            }),
            dataIndex: 'isSubPart',
            render: (text, record) => (
                <FormItem>
                    <WrappedSelect
                        allowClear
                        id={record.id}
                        options={isYesNoToList}
                        value={text}
                        onChange={v => this.onUpdateisSubPart(record, v)}/>
                </FormItem>
            )
        },
        {
            title: formatMessage({
                id: 'detailPanel.column.supplierCode',
                defaultMessage: '供应商代码'
            }),
            dataIndex: 'supplierCode',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.shipPartId}
                        name="supplierCode"
                        value={text}
                        onBlur={v => this.onChangeSupplierCode(record, v)} />
                </FormItem>
            )
        },
        {
            title: formatMessage({
                id: 'newPanel.validation.option',
                defaultMessage: '操作'
            }),
            dataIndex: 'shipPartId',
            fixed: 'right',
            width: FIXED_COLUMN_WIDTH,
            render: (text, record) => <a data-id={text} onClick={() => this.onDeletePartDetail(record)}>{formatMessage({
                id: 'newPanel.validation.delete',
                defaultMessage: '删除'
            })}</a>
        }];
        const detailComp = [];
        details.forEach(item =>
            detailComp.push(<Panel header={item.kdBomCode} key={item.id}
                extra={<div>
                    <Button
                        size="small"
                        id={item.id}
                        onClick={this.onClickAddPartDetailBtn}>
                        {formatMessage({
                            id: 'addPanel.btn.addPart',
                            defaultMessage: '新增备件清单'
                        })}
                    </Button>
                    <Button
                        size="small"
                        id={item.id}
                        onClick={this.onDeleteDetail}>
                        {formatMessage({
                            id: 'newPanel.operation.delete',
                            defaultMessage: '删除'
                        })}
                    </Button>
                </div>}>
                <DescriptionList size="large">
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'addPanel.data.quantity',
                            defaultMessage: '索赔数量'
                        })}{...FORM_OPTIONS.item} validateStatus={isValidate && !item.quantity ? 'error' : null} required>
                            <WrappedInputNumber
                                name="quantity"
                                value={item.quantity}
                                onBlur={this.onChangeQuantity} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'addPanel.data.productCategoryId',
                            defaultMessage: '车型'
                        })}{...FORM_OPTIONS.item} validateStatus={isValidate && !item.productCategoryId ? 'error' : null} required>
                            <SearchInput
                                disabled={true}
                                value={item.productCategoryName}
                                onClickSearchBtn={() => this.onClickSearchProductBtn(item.id)} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'addPanel.data.engineCode',
                            defaultMessage: '发动机号'
                        })}{...FORM_OPTIONS.item}>
                            <TextInput
                                name="engineCode"
                                value={item.engineCode}
                                onBlur={this.onChangeAll} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'addPanel.data.transmissionSn',
                            defaultMessage: '变速箱号'
                        })}{...FORM_OPTIONS.item}>
                            <TextInput
                                name="transmissionSn"
                                value={item.transmissionSn}
                                onBlur={this.onChangeAll} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'addPanel.data.vehicleFrameCode',
                            defaultMessage: '车架码'
                        })}{...FORM_OPTIONS.item}>
                            <TextInput
                                name="vehicleFrameCode"
                                value={item.vehicleFrameCode}
                                onBlur={this.onChangeAll} />
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'addPanel.data.engineModel',
                            defaultMessage: '发动机型号'
                        })}{...FORM_OPTIONS.item}>
                            {item.engineModel}
                        </FormItem>
                    </Col>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem label={formatMessage({
                            id: 'addPanel.data.transmissionModel',
                            defaultMessage: '变速箱型号'
                        })}{...FORM_OPTIONS.item}>
                            {item.transmissionModel}
                        </FormItem>
                    </Col>
                </DescriptionList>
                <div className={styles.description}>
                    <Table
                        className="white-space-nowrap"
                        columns={partColumns}
                        rowKey="shipPartId"
                        pagination={false}
                        dataSource={item.partDetails}
                        {...TABLE} />
                </div>
            </Panel>
            ));
        return (
            <div className="form-standard">
                <Spin spinning={this.state.loading}>
                    <Card title={formatMessage({
                        id: 'addPanel.column.common',
                        defaultMessage: '索赔管理信息'
                    })}>
                        <Form>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.code',
                                        defaultMessage: '申请单号'
                                    })}{...FORM_OPTIONS.item}>
                                        {this.props.id
                                            ? data.code
                                            : <span className="value-font">{formatMessage({
                                                id: 'addPanel.value.defalut',
                                                defaultMessage: '尚未生成'
                                            })}</span>}
                                    </FormItem>
                                </Col>
                                {brands && brands.length > 1 && <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.brandName',
                                        defaultMessage: '品牌'
                                    })}{...FORM_OPTIONS.item} validateStatus={isValidate && !data.brandId ? 'error' : null} required>
                                        {this.props.id
                                            ? `(${data.brandCode})${data.brandCode}`
                                            : <WrappedSelect
                                                name="brandId"
                                                value={data.brandId}
                                                options={brands}
                                                onChange={this.onChange}/>}
                                    </FormItem>
                                </Col>}
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.form.dealerId',
                                        defaultMessage: '总代'
                                    })}{...FORM_OPTIONS.item} required>
                                        {this.props.id
                                            ? `(${data.dealerCode})${data.dealerName}`
                                            : dealerInfo}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.country',
                                        defaultMessage: '国家'
                                    })} {...FORM_OPTIONS.item}>
                                        {this.props.id
                                            ? data.country
                                            : countryInfo}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.contactNumber',
                                        defaultMessage: '联系电话'
                                    })} {...FORM_OPTIONS.item}>
                                        {data.contactNumber}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.claimType',
                                        defaultMessage: '索赔类型'
                                    })} {...FORM_OPTIONS.item} validateStatus={isValidate && !data.claimType ? 'error' : null} required>
                                        <WrappedSelect
                                            allowClear
                                            name="claimType"
                                            options={kDClaimTypeToList}
                                            value={data.claimType}
                                            onChange={this.onChange}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.claimPartType',
                                        defaultMessage: '索赔配件类型'
                                    })} {...FORM_OPTIONS.item} validateStatus={isValidate && !data.claimPartType ? 'error' : null} required>
                                        <WrappedSelect
                                            allowClear
                                            name="claimPartType"
                                            options={claimPartTypeToList}
                                            value={data.claimPartType}
                                            onChange={this.onChange}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.data.saleShipmentId',
                                        defaultMessage: '发运单编号'
                                    })}{...FORM_OPTIONS.item} validateStatus={isValidate && !data.saleShipmentId ? 'error' : null} required>
                                        {this.props.id
                                            ? data.saleShipmentCode
                                            : <SearchInput
                                                disabled={true}
                                                value={data.saleShipmentCode}
                                                onClickSearchBtn={() => this.onClickSearchSaleShipmentBtn()} />}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.batchNumber',
                                        defaultMessage: '批次号'
                                    })} {...FORM_OPTIONS.item} validateStatus={isValidate && !data.batchNumber ? 'error' : null} required>
                                        <TextInput
                                            name="batchNumber"
                                            value={data.batchNumber}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.urgencyLevel',
                                        defaultMessage: '紧急程度'
                                    })} {...FORM_OPTIONS.item} validateStatus={isValidate && !data.urgencyLevel ? 'error' : null} required>
                                        <WrappedSelect
                                            allowClear
                                            name="urgencyLevel"
                                            options={urgencyLevelToList}
                                            value={data.urgencyLevel}
                                            onChange={this.onChange}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.requireCompensationWay',
                                        defaultMessage: '需求赔付方式'
                                    })} {...FORM_OPTIONS.item} validateStatus={isValidate && !data.requireCompensationWay ? 'error' : null} required>
                                        <WrappedSelect
                                            allowClear
                                            name="requireCompensationWay"
                                            options={claimCompensationWayToList}
                                            value={data.requireCompensationWay}
                                            onChange={this.onChange}/>
                                    </FormItem>
                                </Col>
                                {data.requireCompensationWay === 2 && <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.requireTransportType',
                                        defaultMessage: '需求运输方式'
                                    })} {...FORM_OPTIONS.item} validateStatus={isValidate && !data.requireTransportType ? 'error' : null} required>
                                        <WrappedSelect
                                            allowClear
                                            name="requireTransportType"
                                            options={claimTransportTypeToList}
                                            value={data.requireTransportType}
                                            onChange={this.onChange}/>
                                    </FormItem>
                                </Col>}
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.containerNumber',
                                        defaultMessage: '集装箱号'
                                    })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="containerNumber"
                                            value={data.containerNumber}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.boxNumber',
                                        defaultMessage: '纸箱号'
                                    })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="boxNumber"
                                            value={data.boxNumber}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.caseNumber',
                                        defaultMessage: '箱号'
                                    })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="caseNumber"
                                            value={data.caseNumber}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.bLNumber',
                                        defaultMessage: '提单号'
                                    })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="bLNumber"
                                            value={data.bLNumber}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.bLDate',
                                        defaultMessage: '提单日期'
                                    })} {...FORM_OPTIONS.item}>
                                        <WrappedDatePicker
                                            name="bLDate"
                                            value={data.bLDate}
                                            onChange={this.onChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row className="row-margin">
                                <Col>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.description',
                                        defaultMessage: '索赔详细描述'
                                    })} {...FORM_ROW_OPTIONS.item} validateStatus={isValidate && !data.description ? 'error' : null} required>
                                        <TextInput
                                            name="description"
                                            type="textarea"
                                            value={data.description}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row className="row-margin">
                                <Col>
                                    <FormItem label={formatMessage({
                                        id: 'addPanel.operation.remark',
                                        defaultMessage: '备注'
                                    })} {...FORM_ROW_OPTIONS.item}>
                                        <TextInput
                                            name="remark"
                                            type="textarea"
                                            value={data.remark}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                        </Form>
                    </Card>
                    <Card
                        title={formatMessage({
                            id: 'addPanel.operation.detail',
                            defaultMessage: 'KD索赔清单'
                        })}>
                        <Button
                            type="dashed"
                            icon="plus"
                            block
                            disabled={!data.saleShipmentId}
                            className={styles.plusBtn}
                            onClick={this.onClickAddDetailBtn}>
                            {formatMessage({
                                id: 'addPanel.couponInfo.operation.new',
                                defaultMessage: '新增'
                            })}
                        </Button>
                        <Collapse>
                            {detailComp}
                        </Collapse>
                    </Card>
                    <Card title={formatMessage({
                        id: 'addPanel.title.claimDetailFiles',
                        defaultMessage: '索赔资料附件清单'
                    })}>
                        <Row>
                            <Col {...FORM_OPTIONS.col}>
                                <FileUpload onChange={this.onChangeUpload}
                                    onRemove={this.removeFilePath}
                                    fileList={data.claimDetails || []}/>
                            </Col>
                        </Row>
                    </Card>
                    <Card className={styles.optionBtn}>
                        <Button
                            type="primary"
                            onClick={this.onSubmit}
                            loading={loading}>
                            {formatMessage({
                                id: 'addPanel.operation.onSubmit',
                                defaultMessage: '保存'
                            })}
                        </Button>
                    </Card>
                </Spin>
                {
                    this.state.showSaleShipmentSelectPanel &&
                    <SaleShipmentSelectPanel
                        selectKey={data.saleShipmentId}
                        dealerId={enterprise.dealerId}
                        brandId={brandIdId}
                        onCancel={this.onCloseSaleShipmentSelectPanel}
                        onSelect={this.onSelectSaleShipment} />
                }
                {
                    this.state.showSaleShipmentDetailSelectPanel && <SaleShipmentDetailSelectPanel
                        saleShipmentId={data.saleShipmentId}
                        selectKey={details.id}
                        onCancel={this.onClickAddDetailBtn}
                        onSelect={this.onSelectSaleShipmentDetail} />
                }
                {
                    this.state.showPartSelectPanel && <PartSelectPanel
                        currentSelectRowkeys={currentSelectRowkeys}
                        brandId={brandIdId}
                        selectKey={details.id}
                        onCancel={this.onClickClosePartDetailBtn}
                        onSelect={this.onSelectPart} />
                }
                {
                    this.state.showPartOneSelectPanel && <PartOneSelectPanel
                        brandId={brandIdId}
                        selectKey={details.id}
                        onCancel={this.onClickOneDetailCloseBtn}
                        onSelect={this.onSelectPartDetail} />
                }
                {
                    this.state.showProductSelectPanel && <ProductSelectPanel
                        brandId={brandIdId}
                        dealerId={enterprise.dealerId}
                        selectKey={details.id}
                        onCancel={this.onClickAddProductDetailBtn}
                        onSelect={this.onSelectProduct} />
                }
                <div className="page-toolbar">
                    <Return
                        disabled={this.state.loading}
                        onConfirm={this.onClickOkOnPopconfirm}
                        showWarning={this.state.hasOperat} />
                </div>
            </div>
        );
    }
}

AddPanel.propTypes = {
    successReturn: PropTypes.func.isRequired,
    onCancel: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    onSubmit: PropTypes.func.isRequired,
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    tableSearch,
    saveQueryDealer,
    onNewUpdate
} from './actions';
import {createSelector} from 'reselect';

const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const enterprise = initData.enterprise;
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `(${item.code})${item.name}`,
            value: item.id
        })) : [];
        return {
            brands,
            enterprise
        };
    }
);
const mapStateToProps = state => ({
    initEnumData: getinitEnumData(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    onSelect: data => dispatch(saveQueryDealer(data)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onSubmit: (data, details, brandIdId, enterprise) =>
        dispatch(onNewUpdate(data, details, brandIdId, enterprise)).then(result => {
            if(result.success) {
                dispatch(tableSearch());
                props.history.push(routes.query.url());
            }
        }),
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(AddPanel));
